package com.meelive.ingkee.push.platform.xiaomi;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.meelive.ingkee.push.InkePushHelper;
import com.meelive.ingkee.push.model.InkePushType;
import com.meelive.ingkee.push.common.PushUtils;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.List;

import static com.meelive.ingkee.push.model.Constance.TAG;

/**
 * Create by: fanfeiyue
 * on: 2019-08-22
 * desc:
 */
public class InkeMiMsgReceiver extends PushMessageReceiver {

    /**
     * 接受透传消息
     *
     * @param context
     * @param miPushMessage
     */
    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage miPushMessage) {
        if (context == null || miPushMessage == null || TextUtils.isEmpty(miPushMessage.getContent())) {
            return;
        }
        if (PushUtils.DEBUG) {
            Log.d(TAG, "InkeMiMsgReceiver onReceivePassThroughMessage is called. " + miPushMessage.toString());
        }
        InkePushHelper.getInstance().onPassThroughPush(context, miPushMessage.getContent(), InkePushType.MIPUSH);
    }

    /**
     * 接收服务器发来的通知栏消息（用户点击通知栏时触发）
     *
     * @param context
     * @param miPushMessage
     */
    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
        if (PushUtils.DEBUG) {
            Log.d(TAG, "InkeMiMsgReceiver onNotificationMessageClicked is called. " + miPushMessage.toString());
        }
        if (context == null) {
            return;
        }
        InkePushHelper.getInstance().onNotificationClick(context, miPushMessage.getContent(), InkePushType.MIPUSH);
    }

    /**
     * 接收服务器发来的通知栏消息（消息到达客户端时触发，并且可以接收应用在前台时不弹出通知的通知消息）
     *
     * @param context
     * @param miPushMessage
     */
    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
        if (PushUtils.DEBUG) {
            Log.d(TAG, "InkeMiMsgReceiver onNotificationMessageArrived is called. " + miPushMessage.toString());
        }
        if (context == null) {
            return;
        }
        InkePushHelper.getInstance().onNotificationArrived(context, miPushMessage.getContent(), InkePushType.MIPUSH);
    }

    /**
     * 接受客户端向服务器发送注册命令消息后返回的响应
     *
     * @param context
     * @param miPushCommandMessage
     */
    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage miPushCommandMessage) {
        String command = miPushCommandMessage.getCommand();
        List<String> arguments = miPushCommandMessage.getCommandArguments();
        String regId = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (miPushCommandMessage.getResultCode() == ErrorCode.SUCCESS) {
                InkePushHelper.getInstance().onReceiveToken(InkePushType.MIPUSH, regId);
            } else {
                InkePushHelper.getInstance().onErrorResponse(InkePushType.MIPUSH, miPushCommandMessage.getResultCode(), miPushCommandMessage.toString());
            }
        }
    }

//    /**
//     * 接收客户端向服务器发送命令消息后返回的响应
//     * @param context
//     * @param message
//     */
//    @Override
//    public void onCommandResult(Context context, MiPushCommandMessage message) {
//        String command = message.getCommand();
//        List<String> arguments = message.getCommandArguments();
//        String cmdArg1 = arguments != null && arguments.size() > 0 ? (String) arguments.get(0) : null;
//        String cmdArg2 = arguments != null && arguments.size() > 1 ? (String) arguments.get(1) : null;
//        Log.i(TAG, "onReceiveRegisterResult. cmdArg1: " + cmdArg1 + "; cmdArg2:" + cmdArg2);
//        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
//            if (message.getResultCode() == ErrorCode.SUCCESS) {
//                InkePushHelper.getInstance().onReceiveToken(InkePushType.MIPUSH, cmdArg1);
//            } else {
//                InkePushHelper.getInstance().onErrorResponse(InkePushType.MIPUSH, message.getResultCode());
//            }
//        }
//    }
}
